package com.pick.admin.controller;

import com.pick.base.common.Result;
import com.pick.admin.dto.AssignRolesRequest;
import com.pick.admin.dto.AssignUserPermissionsRequest;
import com.pick.admin.dto.AssignMenuPermissionsRequest;
import com.pick.admin.entity.SysMenu;
import com.pick.admin.service.SysUserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 用户角色关联控制器
 *
 * @author pick
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/system/user")
@Tag(name = "用户角色管理", description = "用户角色分配相关接口")
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService userRoleService;

    /**
     * 为用户分配角色
     */
    @PostMapping("/assign-roles")
    @Operation(summary = "分配用户角色")
    @SaCheckPermission("system:user:assign-roles")
    public Result<Void> assignRoles(@RequestBody AssignRolesRequest request) {
        userRoleService.assignRoles(request.getUserId(), request.getRoleIds());
        return Result.success();
    }

    /**
     * 获取用户的角色列表
     */
    @GetMapping("/{userId}/roles")
    @Operation(summary = "获取用户角色")
    @SaCheckPermission("system:user:view")
    public Result<List<Long>> getUserRoles(@PathVariable Long userId) {
        List<Long> roleIds = userRoleService.getUserRoleIds(userId);
        return Result.success(roleIds);
    }

    /**
     * 获取用户的权限列表
     */
    @GetMapping("/{userId}/permissions")
    @Operation(summary = "获取用户权限")
    @SaCheckPermission("system:user:view")
    public Result<List<Long>> getUserPermissions(@PathVariable Long userId) {
        List<Long> permissionIds = userRoleService.getUserPermissionIds(userId);
        return Result.success(permissionIds);
    }

    /**
     * 为用户分配权限
     */
    @PostMapping("/assign-permissions")
    @Operation(summary = "分配用户权限")
    @SaCheckPermission("system:user:assign-permissions")
    public Result<Void> assignUserPermissions(@RequestBody AssignUserPermissionsRequest request) {
        userRoleService.assignUserPermissions(request.getUserId(), request.getPermissionIds());
        return Result.success();
    }

    /**
     * 移除用户角色
     */
    @DeleteMapping("/{userId}/roles/{roleId}")
    @Operation(summary = "移除用户角色")
    @SaCheckPermission("system:user:assign-roles")
    public Result<Void> removeUserRole(@PathVariable Long userId, @PathVariable Long roleId) {
        userRoleService.removeUserRole(userId, roleId);
        return Result.success();
    }

    /**
     * 批量移除用户角色
     */
    @DeleteMapping("/{userId}/roles")
    @Operation(summary = "批量移除用户角色")
    @SaCheckPermission("system:user:assign-roles")
    public Result<Void> removeUserRoles(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        userRoleService.removeUserRoles(userId, roleIds);
        return Result.success();
    }

    /**
     * 为用户分配菜单权限
     */
    @PostMapping("/assign-menus")
    @Operation(summary = "分配用户菜单权限")
    @SaCheckPermission("system:user:assign-menus")
    public Result<Void> assignUserMenus(@RequestBody AssignMenuPermissionsRequest request) {
        userRoleService.assignUserMenus(request.getUserId(), request.getMenuIds());
        return Result.success();
    }

    /**
     * 获取用户的菜单列表
     */
    @GetMapping("/{userId}/menus")
    @Operation(summary = "获取用户菜单")
    @SaCheckPermission("system:user:view")
    public Result<List<SysMenu>> getUserMenus(@PathVariable Long userId) {
        List<SysMenu> menus = userRoleService.getUserMenus(userId);
        return Result.success(menus);
    }

    /**
     * 为角色分配菜单权限
     */
    @PostMapping("/role/assign-menus")
    @Operation(summary = "分配角色菜单权限")
    @SaCheckPermission("system:role:assign-menus")
    public Result<Void> assignRoleMenus(@RequestBody AssignMenuPermissionsRequest request) {
        userRoleService.assignRoleMenus(request.getRoleId(), request.getMenuIds());
        return Result.success();
    }

    /**
     * 获取角色的菜单列表
     */
    @GetMapping("/role/{roleId}/menus")
    @Operation(summary = "获取角色菜单")
    @SaCheckPermission("system:role:view")
    public Result<List<SysMenu>> getRoleMenus(@PathVariable Long roleId) {
        List<SysMenu> menus = userRoleService.getRoleMenus(roleId);
        return Result.success(menus);
    }
}
